﻿using System.Windows;
using System.Windows.Controls;
using Models;
using System;

namespace SuperQuery.UserControls.AddDatabaseWizard
{
    /// <summary>
    /// Interaction logic for ServerDetailsPage.xaml
    /// </summary>
    public partial class ServerDetailsPage : UserControl, WizardControl.Views.IPage, Views.IServerDetailsView
    {
        /// <summary>
        /// Constructor for the server details Wizard page
        /// </summary>
        public ServerDetailsPage()
        {
            InitializeComponent();

            // Hook up the DataContext so that databinding will work
            DataContext = this;

            m_Presenter = new Presenters.ServerDetailsPresenter(this);
        }

        #region Implementation of IPage

        /// <summary>
        /// An object containing data that this page can use
        /// </summary>
        public object InputData
        {
            get;
            set;
        }

        /// <summary>
        /// An object containing data that will be passed to the InputData property of the next page
        /// </summary>
        public object OutputData
        {
            get
            {
                // Return the server details
                return m_Presenter.GetData();
            }
            set
            {
                // This is a read only property for this page so do nothing
                // This "set" section has to be here becuase of the IPage interface
            }
        }

        /// <summary>
        /// The wizard control that this page belongs to
        /// </summary>
        public WizardControl.Wizard ParentWizard
        {
            get;
            set;
        }

        /// <summary>
        /// The number of this page
        /// </summary>
        /// <returns>A page number</returns>
        public int PageNumber()
        {
            return 2;
        }

        /// <summary>
        /// The caption to use for this page
        /// </summary>
        /// <returns>A page caption</returns>
        public string PageCaption()
        {
            return "Database server connection settings";
        }

        /// <summary>
        /// The state of the Finish button
        /// </summary>
        /// <returns>If true then Finishg will be active</returns>
        public bool AllowFinish()
        {
            return false;
        }

        /// <summary>
        /// The state of the Cancel button
        /// </summary>
        /// <returns>If true then the Cancel button will be active</returns>
        public bool AllowCancel()
        {
            return true;
        }

        /// <summary>
        /// The state of the Back button
        /// </summary>
        /// <returns>If true then the Back button will be active</returns>
        public bool AllowBack()
        {
            return true;
        }

        /// <summary>
        /// The state of the Forward button
        /// </summary>
        /// <returns>If true then the Forward button will be active</returns>
        public bool AllowForward()
        {
            return true;
        }

        /// <summary>
        /// Validate data on the current page
        /// </summary>
        /// <returns>If true then the next page will be displayed, otherwise we remain on the current page</returns>
        public bool ValidatePage()
        {
            return m_Presenter.ValidateServer();
        }

        /// <summary>
        /// The next page to progress to when forward is clicked
        /// </summary>
        /// <returns>The next page number</returns>
        public int NextPageNumber()
        {
            return WizardControl.Wizard.FINISH_PAGE;
        }

        /// <summary>
        /// Called just before the page is displayed
        /// </summary>
        public void PageActivated()
        {
            // Clear any cached information on the server details page presenter. This is so that if the user
            // has changed the plug in that they will be useing it will be loaded correctly
            m_Presenter.ClearCache();

            // Load and display the user interface from the plug in
            serverDetailPageContent.Content = m_Presenter.UserInterface;
        }

        /// <summary>
        /// Called just after the page is validated, before the next page is displayed
        /// </summary>
        public void PageDeactivated()
        {
            // Nothing to do here for this page
            return;
        }

        #endregion

        #region Implementation of IServerDetailsView

        /// <summary>
        /// The server plug in that we using
        /// </summary>
        public Guid ServerPluginID
        {
            get
            {
                // The server plug in ID will be in the input data we receive from the previous Wizard Page
                return (Guid)InputData;
            }
            set
            {
                // Do nothing
            }
        }

        #endregion

        /// <summary>
        /// Presenter for this class
        /// </summary>
        private readonly Presenters.ServerDetailsPresenter m_Presenter;
    }
}
